<?php


namespace app\system\validate;

class Menu extends \think\Validate
{
	protected $rule = ["name" => "require", "disable" => "require|in:0,1", "uri" => "uri", "type" => "type", "sort" => "integer|between:1,1000"];
	protected $message = ["name.require" => "菜单名称不能为空", "uri.uri" => "地址不存在", "sort.integer" => "排序必须为正整数", "sort.between" => "排序范围必须在1到1000之间"];
	public function uri($uri, $other, $data)
	{
		if ($uri === "") {
			return true;
		}
		if ($data["type"] == 1) {
			return true;
		}
		if (count(explode("/", $uri)) != 2) {
			return false;
		}
		define("no_authentication", true);
		list($controller, $function) = explode("/", $uri);
		if ($controller === parseCamel($controller, true)) {
			return false;
		}
		$controller_tmp = explode(".", $controller);
		$controller_complete = "app\\system\\controller\\";
		if (is_array($controller_tmp) && count($controller_tmp) == 2) {
			$controller_complete .= $controller_tmp[0] . "\\" . parseCamel($controller_tmp[1], true);
		} else {
			$controller_complete .= parseCamel($controller, true);
		}
		if (!class_exists($controller_complete)) {
			return false;
		}
		$class = new $controller_complete();
		if (!method_exists($class, $function)) {
			return false;
		}
		return true;
	}
	public function type($type, $other, $data)
	{
		if ($type == 2 && $data["pid"] == 0) {
			return "类型为权限的不能设置为顶级";
		}
		$data["id"] = empty($data["id"]) ? 0 : $data["id"];
		$result = \think\Db::name("system_admin_menu")->where("id", "<>", $data["id"])->where(["pid" => $data["pid"]])->value("type");
		if (!empty($result) && $result != $data["type"]) {
		}
		return true;
	}
}